package gofish.servlets;

import gofish.objects.Client;

import java.util.Scanner;

/**
 * Simulation is used to simulate a game of Go Fish.  Rather than the game
 * taking place over a network like it will eventually, this allows it to
 * takes place over the command-line.
 * @author Matt Lauber
 *
 */
public class Simulation {
	
	private static final boolean FRANK = true;

	/**
	 * main() simply creates two clients (Frank and Bob), makes Frank start
	 * a group, and lets Bob and Frank join the group.
	 * Once they're both in the game, a loop starts in which they both call
	 * refresh() on each iteration, and one of them will call takeTurn().
	 * A turn is taken by typing in the 2-character representation of the card
	 * that you wish to ask for, and pressing enter.  The interface is very annoying
	 * because every JSON response is printed every single time, but this had to be
	 * done to make sure they printed the correct information. 
	 * @param args
	 */
	public static void main(String[] args) {
		
		Client frank = new Client("Frank", "Bob");
		Client bob = new Client("Bob", "Frank");
		
		frank.startGame();
		frank.joinGroup();
		bob.joinGroup();
		
		int eid = -1;
		boolean turn = FRANK;
		do {
			frank.refresh();
			eid = frank.getEid();
			//System.out.println("eid: " + eid);
			
			bob.refresh();
			
			if (eid == -1) {
				break;
			}
			
			Scanner keyboard = new Scanner(System.in);
			int result = -1;
			String input;
			
			if (turn == FRANK) {
				
				while (result == -1) {
					System.out.println("Frank: ");
					input = keyboard.nextLine();
					result = frank.takeTurn(input);
				}
			}
			else {
				
				while (result == -1) {
					System.out.println("Bob: ");
					input = keyboard.nextLine();
					result = bob.takeTurn(input);
				}
			}
			
			if (result == 2 || result == 3) {
				turn = !turn;
			}
		} while (true);
	}
}
